package com.jiawei.message.decode;

import org.smartboot.socket.Protocol;
import org.smartboot.socket.transport.AioSession;

import java.nio.ByteBuffer;

/**
 * @author : willian fu
 * @version : 1.0
 * 字符串解码器
 */
public class StringProtocol implements Protocol<String> {

    @Override
    public String decode(ByteBuffer readBuffer, AioSession<String> aioSession) {
        byte length = readBuffer.get(readBuffer.position());//1
        System.out.println("收到"+length+"个字节");
        if (length+1 < readBuffer.remaining()) {//2
            return null;
        }
        try {
            byte[] b = new byte[readBuffer.get()];//3
            readBuffer.get(b);//4
            return new String(b);//5
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
